/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 刘远望
 * Date: 2025-05-21
 * Time: 21:45
 */
//滑动窗口 无重复字符的最长字串
class Solution {
    public int lengthOfLongestSubstring(String s) {
        int n = s.length();
        //将字符串变成字符数组
        char[] arr = s.toCharArray();
        //运用ascii值作为下标来记录字符出现的次数
        int[] hash = new int[128];
        int maxLen = 0;
        for (int right = 0, left = 0; right < n; right++) {
            hash[arr[right]]++;//进窗口
            while (hash[arr[right]] > 1) {//判断
                //每次left右移都要把前面移出字符出现的次数减去
                hash[arr[left++]]--;//出窗口--判断--出窗口
                //left++;
            }
            maxLen = Math.max(maxLen, right - left + 1);//更新结果
        }
        return maxLen;
    }
}